(function() {
    angular.module('app').factory('navComponentService', navComponentService);

    navComponentService.$inject = ['util', '$q','$http','$window'];

    function navComponentService(util, $q,$http,$window) {

        return {
            loginOut: _loginOut,
            getUser: _getUser
        }

        function _loginOut(user) {

            var channel = user.channel;

            switch (channel) {

                case '0': //趋数

                    $http.get('/marketconditions/user/userLoginOut.htm').success(function(resp) {
                        if (window.location.href.indexOf('121.41.160') != -1) {
                            $window.location.href = 'login.html';
                        } else if (window.location.href.indexOf('localhost') != -1) {
                            $window.location.href = 'login.html';
                        } else {
                            $window.location.href = 'login.html';
                        }

                    });
                    break;
                case '1': //云起
                    $.ajax({
                        url: "http://openapi.shopex.cn/oauth/logout",
                        dataType: 'jsonp',
                        type: "GET",
                        success: function(result) {}
                    }).done().fail().always(function(data) {
                        if (data.status == '200') {
                            $http.get('/marketconditions/user/shopexLoginOut.htm').success(function(resp) {
                                $window.location.href = '/marketconditions/user/shopexLogin.htm';
                            });

                        }
                    });
                    break;
                case '2': //电商先生
                    $http.get('/marketconditions/user/userLoginOut.htm').success(function(resp) {

                        $window.location.href = 'login.html';

                    });
                    break;
                case '3': //品谱大数据
                    $http.get('/marketconditions/user/userLoginOut.htm').success(function(resp) {

                        $window.location.href = 'login.html';

                    });
                    break;
                default:
                    $http.get('/marketconditions/user/userLoginOut.htm').success(function(resp) {

                        $window.location.href = 'login.html';

                    });
                    break;

            }
        }

        function _getUser(){

        	var defer = $q.defer();

        	util.$get({
        		url:'/marketconditions/index/getUser.htm'
        	}).then(function(resp){

        		defer.resolve(resp.value);


        	},function(err){

        		defer.reject();

        	});


        	return defer.promise;
        }
    }
})();
